package com.example.administrator.myapplication.View.Activity;

import android.graphics.Color;
import android.os.Build;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.administrator.myapplication.Bean.User;
import com.example.administrator.myapplication.Presenter.MinePresenter;
import com.example.administrator.myapplication.Util.SharedPreferencesUtil;
import com.example.administrator.myapplication.View.Adapter.MyFragmentAdapter;
import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.View.Fragment.IMineView;
import com.example.administrator.myapplication.View.Fragment.LabelFragment;
import com.example.administrator.myapplication.View.Fragment.MineFragment;
import com.example.administrator.myapplication.View.Fragment.NoticeFragment;
import com.example.administrator.myapplication.View.Fragment.TagFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener,IMineView {
    private View view_status;
    private List<String> fragments_title = new ArrayList<>();
    private ViewPager vp_content;
    private RadioButton bz_btn;
    private RadioButton ls_btn;
    private RadioButton tz_btn;
    private RadioButton wd_btn;
    private RadioGroup rg;
    private MinePresenter minePresenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){
            getWindow().setStatusBarColor(Color.parseColor("#d16d26"));
        }
        initView();
        initContentFragment();
    }

    @Override
    protected void onRestart(){
        super.onRestart();
        int current = vp_content.getCurrentItem();
        super.recreate();
        vp_content.setCurrentItem(current);
    }

    public void  initView(){
        view_status = (View)findViewById(R.id.view_status);
        vp_content = (ViewPager)findViewById(R.id.vp_content);
        rg = (RadioGroup)findViewById(R.id.tabs_rg);
        bz_btn = (RadioButton)findViewById(R.id.bz_tab);
        ls_btn = (RadioButton)findViewById(R.id.ls_tab);
        tz_btn = (RadioButton)findViewById(R.id.tz_tab);
        wd_btn = (RadioButton)findViewById(R.id.wd_tab);
        rg.setOnCheckedChangeListener(mOnCheckedChangeListener);
        minePresenter = new MinePresenter(this);
        minePresenter.getProfile();
    }
    public void initContentFragment(){
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new TagFragment());
        fragments.add(new LabelFragment());
        fragments.add(new NoticeFragment());
        fragments.add(new MineFragment());
        MyFragmentAdapter adapter = new MyFragmentAdapter(getSupportFragmentManager(),fragments);
        vp_content.setAdapter(adapter);
        vp_content.setOffscreenPageLimit(0);
        vp_content.addOnPageChangeListener(this);
        ActionBar actionBar = getSupportActionBar();
        if(actionBar!=null) {
            actionBar.setDisplayShowTitleEnabled(false);
        }
        vp_content.setCurrentItem(0);
        onPageSelected(0);
    }

    private RadioGroup.OnCheckedChangeListener mOnCheckedChangeListener = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            for (int i = 0; i < group.getChildCount(); i++) {
                if (group.getChildAt(i).getId() == checkedId) {
                    vp_content.setCurrentItem(i);
                    return;
                }
            }
        }
    };

    @Override
    public void showProfile(User user) {
        SharedPreferencesUtil.saveUser(this,user);
    }
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        RadioButton radioButton = (RadioButton) rg.getChildAt(position);
        radioButton.setChecked(true);
    }

    @Override
    public void showError(String error) {

    }

    @Override
    public void showSuccess(String msg) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
